import { getSceneList, getDeviceList, getSelectList, saveDevice, saveScene, deleteDevice, deleteScene, updateScene, updateDevice, getSensorDesc } from '@/api/innerProject/sceneController'

const state = {
  resData: [],
  tableList: [],
  selectList: [],
  currentList: [],
  projectId: '',
  sceneType: '',
  deviceData: null,
  sceneId: '',
  title: '基坑',
  isAddSensor: false,
  total: '',
  index: ''
}

const mutations = {
  // 更新场景数据
  update_list (state, resData) {
    state.resData = resData
  },
  update_projectParams (state, params) {
    state.projectId = params.projectId
    state.sceneType = params.sceneTypeId
  },
  // 更新添加框的数据
  update_deviceData (state, deviceData) {
    state.deviceData = deviceData
  },
  // 总数
  list_count (state, total) {
    state.total = total
  },
  // 更新传感器表格数据
  update_tableList (state, tableList) {
    state.tableList = tableList
  },
  // 更新传感器类型下拉列表
  update_selectList (state, selectList) {
    state.selectList = selectList
  },
  // 更新是否切换添加场景模块
  update_isAddSensor (state, isAddSensor) {
    state.isAddSensor = isAddSensor
  },
  // 更新当前顶部导航对应数据
  update_currentList (state, currentList) {
    state.currentList = currentList
    state.sceneId = currentList.sceneId
  }
}

const actions = {
  // 刷新列表
  updateResData (context, payload) {
    if (!payload.projectId) {
      payload.projectId = localStorage.getItem('projectId')
    }
    return new Promise((resolve, reject) => {
      getSceneList(payload).then(res => {
        if (res.data.code === (200 || '200')) {
          context.commit('update_projectParams', payload)
          context.commit('update_list', res.data.data)
        }
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  updateTableData (context, payload) {
    if (state.sceneId && !payload) {
      payload = {
        index: 1,
        pageSize: 10,
        sceneId: state.sceneId
      }
    }
    payload.projectId = localStorage.getItem('projectId')
    return new Promise((resolve, reject) => {
      getDeviceList(payload).then(res => {
        // console.log(res)
        if (res.data.code === (200 || '200')) {
          context.commit('update_tableList', res.data.data.data)
          context.commit('list_count', res.data.data.count)
        }
        resolve(res.data.data)
      }).catch(err => {
        reject(err)
      })
    })
  },
  updateSelectData (context, payload) {
    return new Promise((resolve, reject) => {
      getSelectList(payload).then(res => {
        // console.log(res)
        if (res.data.code === (200 || '200')) {
          context.commit('update_selectList', res.data.data)
        }
        resolve(res.data.data)
      }).catch(err => {
        reject(err)
      })
    })
  },
  addDevice (context, payload) {
    return new Promise((resolve, reject) => {
      saveDevice(payload).then(res => {
        if (res.data.code === (200 || '200')) {
          resolve(res)
        }
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  addScene (context, payload) {
    return new Promise((resolve, reject) => {
      saveScene(payload).then(res => {
        if (res.data.code === (200 || '200')) {
          context.commit('update_selectList', res.data.data)
          resolve(res)
        }
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  DeleteDevice (context, payload) {
    return new Promise((resolve, reject) => {
      deleteDevice(payload).then(res => {
        if (res.data.code === (200 || '200')) {
          resolve(res)
        }
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  DeleteScene (context, payload) {
    return new Promise((resolve, reject) => {
      deleteScene(payload).then(res => {
        if (res.data.code === (200 || '200')) {
          resolve(res)
        }
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  UpdateScene (context, payload) {
    return new Promise((resolve, reject) => {
      // console.log(payload)
      updateScene(payload).then(res => {
        if (res.data.code === (200 || '200')) {
          resolve(res)
        }
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  UpdateDevice (context, payload) {
    return new Promise((resolve, reject) => {
      // console.log(payload)
      updateDevice(payload).then(res => {
        if (res.data.code === (200 || '200')) {
          resolve(res)
        }
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  editSensorDesc (context, payload) {
    return new Promise((resolve, reject) => {
      // console.log(payload)
      getSensorDesc(payload).then(res => {
        if (res.data.code === (200 || '200')) {
          resolve(res)
        }
        // resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  }

}
export default {
  namespaced: true,
  actions,
  state,
  mutations
}
